/**
 * 
 */
package fhq.hcmute.modernshopping.fragment;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.adapter.ListProductAdapter;
import fhq.hcmute.modernshopping.adapter.Product;

/**
 * @author: Hien Nguyen Le
 * @emai: hien.nguyenle.it@gmail.com
 * 
 */
public class ShoppingListProduct extends Fragment {

	private View root;
	private ListView lv_ListProduct;
	private Context context;
	private ListProductAdapter listProductAdapter;

	private static ShoppingListProduct instance = null;

	private ShoppingListProduct() {
	}

	public static ShoppingListProduct getInstance() {
		if (instance == null) {
			instance = new ShoppingListProduct();
		}
		return instance;
	}

	public int getTotalQuanlity() {
		return listProductAdapter.getCount();
	}

	public double getTotalPrice() {
		return listProductAdapter.getTotalCost();
	}

	public ShoppingListProduct setContext(Context context) {
		instance.context = context;
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.fragment_shopping_list_product,
				container, false);
		init();
		return root;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getActivity().getMenuInflater().inflate(
				R.menu.context_menu_shopping_list_product, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	private OnItemLongClickListener onStoreItemLongClickListener = new AdapterView.OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int position, long arg3) {
			Product store = listProductAdapter.getItem(position);
			lv_ListProduct.setTag(store);
			return false;
		}
	};

	@Override
	public void onPause() {
		super.onPause();
		Bundle outState = new Bundle();
		onSaveInstanceState(outState);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Product product = (Product) lv_ListProduct.getTag();
		int itemId = item.getItemId();
		switch (itemId) {
		case R.id.menu_item_delete_product:
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle("Xác nhận").setMessage(
					"Bạn có chắc là muốn xóa sản phẩm này");
			builder.setPositiveButton("Xóa", onButtonDialogClickListener);
			builder.setNegativeButton("Đóng", onButtonDialogClickListener);
			builder.create().show();
			break;
		default:
			UpdatePurchaseDialog updatePurchaseDialog = new UpdatePurchaseDialog(
					"Thay đổi", product);
			updatePurchaseDialog.show(getActivity().getFragmentManager(),
					"Dialog");
			break;
		}
		return super.onContextItemSelected(item);
	}

	private DialogInterface.OnClickListener onButtonDialogClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if (which == DialogInterface.BUTTON_POSITIVE) {
				Product product = (Product) lv_ListProduct.getTag();
				deleteProduct(product);
				ShoppingInfo.getInstance().updateView();
			}
		}
	};

	private void init() {
		lv_ListProduct = (ListView) root.findViewById(R.id.lv_list_product);
		this.registerForContextMenu(lv_ListProduct);
		lv_ListProduct.setOnItemLongClickListener(onStoreItemLongClickListener);
		this.listProductAdapter = new ListProductAdapter(context,
				new ArrayList<Product>());
		lv_ListProduct.setAdapter(listProductAdapter);
	}

	public void addProduct(Product product) {
		this.listProductAdapter.add(product);
	}

	public void updatedProduct(Product product) {
		this.listProductAdapter.update(product);
	}

	public void deleteProduct(Product product) {
		this.listProductAdapter.remove(product);
		this.listProductAdapter.notifyDataSetChanged();
	}

	public void refresh() {
		this.listProductAdapter.refresh();
	}
}
